package cn.ffcs.bpmpvc.services.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import cn.ffcs.bpm.model.ProcTranRecord;
import cn.ffcs.bpmpvc.dao.MeetingProtocolDao;
import cn.ffcs.bpmpvc.services.AppBusinessReqService;
import cn.ffcs.bpmpvc.services.MeetingProtocolService;
import cn.ffcs.bpmpvc.services.bean.ConfigBean;
import cn.ffcs.bpmpvc.svc.ProcessPvcService;
import cn.ffcs.bpmpvc.svc.TaskAssignBean;
import cn.ffcs.bpmpvc.svc.TaskService;
import cn.ffcs.bpmpvc.utils.FileUtil;
import cn.ffcs.bpmpvc.web.views.ProcessInstView;
import cn.ffcs.common.dao.AttachDAO;
import cn.ffcs.common.model.Attach;
import cn.ffcs.organ.model.Person;
import cn.ffcs.share.dao.ShareReferenceDAO;
import cn.ffcs.share.model.ShareReference;
import cn.ffcs.system.dict.model.Dict;

public class MeetingProtocolServiceImpl implements MeetingProtocolService {
	private AttachDAO<Attach> attachDAO;
	private ShareReferenceDAO<ShareReference> shareReferenceDAO;
	private ProcessPvcService processPvcService;
	private TaskService taskService;
	private ConfigBean configBean;
	private AppBusinessReqService appBusinessReqService;
	private MeetingProtocolDao meetingProtocolDao;
	public void saveAndStartFlow(String processConfigId, Person startPerson,List<TaskAssignBean> nextTasksList, boolean isCommit,ShareReference shareReference,HttpServletRequest request) throws Exception {
		// 启动流程
		ProcessInstView processInstView = getProcessPvcService().startProcessInst(processConfigId, startPerson, nextTasksList, isCommit);
		
		//对经会阅办进行传阅
		if(isCommit){
			//autoTakeTaskRedo(processInstView.getProcessStatus().getProcinstId());
		}
		
		String sortType=configBean.getMeetingShareSortType();
		//取分类信息
		Dict referenceType=appBusinessReqService.getAppBusinessReqDao().getDictByKey(sortType);
		shareReference.setKeyword(referenceType.getName());
		shareReference.setReferenceType(referenceType);
		shareReference.setProcinstId(processInstView.getProcessStatus().getProcinstId().toString());
		shareReference.setReqCode(appBusinessReqService.getReqCode());
		//保存业务数据
		meetingProtocolDao.save(shareReference);
		//保存附件信息
		FileUtil.saveAttaFiles(request, ShareReference.class.getName(), shareReference.getId().toString());		
	}
	
	public void commitApplyTask(String flowTaskId,String content,String procinstId,List<TaskAssignBean> nextTasksList, boolean isCommit,ShareReference shareReference,HttpServletRequest request) throws Exception{
		ShareReference shareReferenceDb=meetingProtocolDao.getByPID(procinstId);
		shareReferenceDb.setTitle(shareReference.getTitle());
		shareReferenceDb.setContent(shareReference.getContent());
		meetingProtocolDao.save(shareReferenceDb);
		// 删除选择的附件
		String attactIdArr[] = request.getParameterValues("attactId");
		if (attactIdArr != null && attactIdArr.length > 0) {
			for (int i = 0; i < attactIdArr.length; i++) {
				attachDAO.deleteById(attactIdArr[i]);
			}
		}
		// 保存附件数据
		FileUtil.saveAttaFiles(request, ShareReference.class.getName(), shareReferenceDb.getId().toString());
		
		if (isCommit) {
			getTaskService().completeTask(flowTaskId, content, nextTasksList);
			//autoTakeTaskRedo(Integer.valueOf(procinstId));
		} else {
			getTaskService().saveTaskAssignUser(flowTaskId, content, nextTasksList);
		}
	}
	
	public void commitEndProcess(String procinstId){
		getTaskService().getFlowPvcDao().endProcessInst(String.valueOf(procinstId));
		//更新数据
		ShareReference shareReference=meetingProtocolDao.getByPID(procinstId);
		attachDAO.batchDelAttach(ShareReference.class.getName(), shareReference.getId().toString());
		meetingProtocolDao.deleteById(shareReference.getId().toString());
	}
	
	
	private void autoTakeTaskRedo(Integer procinstId){
	 	ProcTranRecord procTranRecord=getTaskService().getFlowPvcDao().getLiveProcTranRecord(procinstId);
	 	//取要传阅的人员信息
	 	String userIds[]=meetingProtocolDao.findPersonList(configBean.getOrgIdPoc(),configBean.getSuperAdminId());
	 	getTaskService().taskReadingToDo(procTranRecord, "传阅", userIds);
	 	//自动结束当前办理的任务
	 	getTaskService().completeTask(procTranRecord, null, null);
	}
	
	public String getProcessId() {
		String name = getConfigBean().getAppMeetinProtocolProcessTag();
		Dict flowDict = appBusinessReqService.getAppBusinessReqDao().getDict(name);
		if (flowDict != null) {
			return flowDict.getKeyValue();
		}
		return "113";
	}
	
	public AttachDAO<Attach> getAttachDAO() {
		return attachDAO;
	}
	public void setAttachDAO(AttachDAO<Attach> attachDAO) {
		this.attachDAO = attachDAO;
	}
	public ShareReferenceDAO<ShareReference> getShareReferenceDAO() {
		return shareReferenceDAO;
	}
	public void setShareReferenceDAO(ShareReferenceDAO<ShareReference> shareReferenceDAO) {
		this.shareReferenceDAO = shareReferenceDAO;
	}
	public ProcessPvcService getProcessPvcService() {
		return processPvcService;
	}
	public void setProcessPvcService(ProcessPvcService processPvcService) {
		this.processPvcService = processPvcService;
	}
	public TaskService getTaskService() {
		return taskService;
	}
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}
	public ConfigBean getConfigBean() {
		return configBean;
	}
	public void setConfigBean(ConfigBean configBean) {
		this.configBean = configBean;
	}
	public AppBusinessReqService getAppBusinessReqService() {
		return appBusinessReqService;
	}
	public void setAppBusinessReqService(AppBusinessReqService appBusinessReqService) {
		this.appBusinessReqService = appBusinessReqService;
	}

	public MeetingProtocolDao getMeetingProtocolDao() {
		return meetingProtocolDao;
	}

	public void setMeetingProtocolDao(MeetingProtocolDao meetingProtocolDao) {
		this.meetingProtocolDao = meetingProtocolDao;
	}
	
	
}
